<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh_cn">
<%--静态包含头部信息--%>
<%@include file="/WEB-INF/include-head.jsp" %>
<link rel="stylesheet" href="crowd/pagination.css">
<link href="ztree/zTreeStyle.css" rel="stylesheet">
<script src="ztree/jquery.ztree.all-3.5.min.js" type="text/javascript"></script>
<script src="jquery/jquery.pagination.js"></script>
<script type="text/javascript" src="crowd/my-role.js" charset="UTF-8"></script>
<script type="text/javascript">

    $(function () {
        // 1.为分页操作准备初始化数据
        window.pageNum = 1;
        window.pageSize = 5;
        window.keyword = "";

        // 调用执行分页的函数
        generatePage();

        // 给查询按钮绑定单击相应函数
        $("#searchBtn").click(function () {
            // // 获取关键词数据复制给全局变量
            window.keyword = $("#keywordInput").val()
            // // 调用分页函数刷新页面
            generatePage();
        });

        // 点击新增按钮打开模态框
        $("#showAddModalBtn").click(function () {
            $("#addModal").modal("show")
        });

        // 给新增模态框中的函数绑定点击事件
        $("#saveRoleBtn").click(function () {
            <%--
            $("#addModal [name=roleName]")
            空格表示后代元素,在后代元素中继续查找
            [name=roleName]为属性选择器
            --%>
            var roleName = $.trim($("#addModal [name=roleName]").val());

            $.ajax({
                url: "role/save.json",
                type: "post",
                data: {
                    "name": roleName
                },
                success: function (resp) {
                    var result = resp.operationResult;
                    if (result == "SUCCESS") {
                        layer.msg("操作成功!")
                        // 将页码定位到最后一页
                        window.pageNum = 999999
                        // 重新加载分页
                        generatePage();
                        // 关闭模态框
                        $("#addModal").modal("hide");
                        // 清理模态框
                        $("#addModal [name=roleName]").val("");
                    }

                    if (result == "FAILED") {
                        layer.msg("操作失败!" + resp.message);
                    }
                },
                error: function (resp) {
                    layer.msg(resp.status + " " + resp.statusText);
                }
            })
        });

        // 给点击更新按钮绑定点击事件，方式一
        $("#rolePageBody").on("click", ".pencilBtn", function () {
            // 打开模态框
            $("#editModal").modal("show");
            // 获取要修改的用户名称
            var roleName = $(this).parent().prev().text();
            // 为了让更新按钮获取用户id，放在全局变量上
            window.roleId = $(this).val();
            // 回显数据
            $("#editModal [name=roleName]").val(roleName);

        })
        <%--方式二
           on绑定事件
           首先找到所有动态生成的元素附着的静态元素
           #rolePageBody 是tbody的id
       --%>
        $("#rolePageBody").on("click", ".pencilBtn", function () {
        })

        // 给更新员工按钮绑定单击事件
        $("#updateRoleBtn").click(function () {
            // 获取新的用户名称
            var roleName = $("#editModal [name=roleName]").val();

            $.ajax({
                url: "role/update.json",
                type: "POST",
                data: {
                    "id": window.roleId,
                    "name": roleName
                },
                dataType: "json",
                success: function (resp) {
                    var result = resp.operationResult;
                    if (result == "SUCCESS") {
                        layer.msg("操作成功!")
                        // 重新加载分页
                        generatePage();
                        // 关闭模态框
                    }

                    if (result == "FAILED") {
                        layer.msg("操作失败!" + resp.operationMessage);
                    }
                },
                error: function (resp) {
                    layer.msg(resp.status + " " + resp.statusText);
                }
            });
            $("#editModal").modal("hide");

        });

        // 给点击删除单个按钮绑定点击事件
        $("#rolePageBody").on("click", ".removeBtn", function () {
            // 获取要删除的用户信息
            var roleId = $(this).val();
            var roleName = $(this).parent().prev().text();
            var roleArray = [{"roleId": roleId, "roleName": roleName}];

            // 弹出确认删除模态框
            showConfirmModel(roleArray);

        });

        // 为 “确认删除” 按钮绑定单击事件
        $("#confirmRemoveBtn").click(function () {

            var arrayStr = JSON.stringify(window.roleIdArray);

            $.ajax({
                url: "role/remove.json",
                type: "post",
                data: arrayStr,
                dataType: "json",
                contentType: "application/json;charset=UTF-8",
                success: function (response) {
                    console.log(response)
                    if (response.operationResult == "SUCCESS") {
                        layer.msg("操作成功！");
                        generatePage();
                    }
                    if (response.operationResult == "FAILED")
                        layer.msg("操作失败" + response.message)
                },
                error: function (response) {
                    layer.msg("statusCode=" + response.status + " message=" + response.statusText);
                }
            });

            // 关闭模态框
            $("#removeModal").modal("hide");
        });


        // 给总的复选框绑定单击事件
        $("#summaryBox").click(function () {
            // 获取当前多选框自身的状态;
            var currentStatus = this.checked;
            // 用当前多选框自身的状态设置其他多选框
            $(".itemBox").prop("checked", currentStatus);
        });

        // 全选 全不选反向操作
        $("#rolePageBody").on("click", ".itemBox", function () {
            // 获取当前已经选中的itembox的数量
            var checkBoxCount = $(".itemBox:checked").length;
            // 获取全部itemBox的数量
            var totalCheckBox = $(".itemBox").length;

            $("#summaryBox").prop("checked", checkBoxCount == totalCheckBox);
        })

        // 给批量删除按钮绑定单击事件
        $("#batchRemoveBtn").click(function () {
            $("#removeModal").modal("show");

            var checkBoxRoleArray = [];

            $(".itemBox:checked").each(function () {
                var roleId = this.id;
                var roleName = $(this).parent().next().text();

                checkBoxRoleArray.push({
                    "roleId": roleId,
                    "roleName": roleName
                })
            });

            if (checkBoxRoleArray.length == 0) {
                layer.msg("请至少选择一个删除!")
                return;
            }

            showConfirmModel(checkBoxRoleArray);

            // 解决全选删除之后，全选框还是为选中状态的BUG
            $("#summaryBox").prop("checked", false);

        });

        // 给分配权限按钮绑定点击事件
        $("#rolePageBody").on("click", ".checkBtn", function () {
            // 打开模态框
            $("#assignModal").modal("show");
            // 保存当前对象id到全局变量中
            window.roleId = this.id;
            // 显示树型结构
            fillAuthTree();
        })

        // 给点击分配角色权限按钮绑定点击事件
        $('#roleAuthBtn').click(function () {

            // 定义用于保存权限id的数组
            var authIdArray = [];

            // 获取zTree对象
            var zTreeObj = $.fn.zTree.getZTreeObj("authTreeDemo");

            // 获取全部被勾选的节点
            var checkedNodes = zTreeObj.getCheckedNodes();

            // 遍历checkNodes
            for (let i = 0; i < checkedNodes.length; i++) {
                var checkNode = checkedNodes[i];
                var authId = checkNode.id;
                authIdArray.push(authId);
            }

            // 统一数据接收类型
            var requestBody = {
                "authIdArray": authIdArray,
                "roleId": [window.roleId]
            }

            var json = JSON.stringify(requestBody);


            $.ajax({
                url: "assign/do/role/assign/auth.json",
                type: "POST",
                contentType: "application/json;charset=utf-8",
                data: json,
                dataType: "json",
                success: function (resp) {
                    console.log(resp)
                    var result = resp.operationResult;
                    if (result == "SUCCESS") {
                        layer.msg("操作成功")
                    }
                    if (result == "FALED") {
                        layer.msg("操作失败")
                    }
                },
                error: function (resp) {
                    layer.msg(resp.status + "" + resp.statusText)
                }
            })

            $("#assignModal").modal("hide")
        });


    });


</script>

<body>
<%--静态包含导航栏--%>
<%@include file="/WEB-INF/include-nav.jsp" %>
<div class="container-fluid">
    <div class="row">

        <%--        静态包含菜单栏--%>
        <%@include file="/WEB-INF/include-sidebar.jsp" %>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="glyphicon glyphicon-th"></i> 数据列表</h3>
                </div>
                <div class="panel-body">
                    <form class="form-inline" role="form" style="float:left;">
                        <div class="form-group has-feedback">
                            <div class="input-group">
                                <div class="input-group-addon">查询条件</div>
                                <input id="keywordInput" class="form-control has-success" name="keyword" type="text"
                                       placeholder="请输入查询条件">
                            </div>
                        </div>
                        <button type="button" id="searchBtn" class="btn btn-warning"><i
                                class="glyphicon glyphicon-search"></i> 查询
                        </button>
                    </form>
                    <button type="button" id="batchRemoveBtn" class="btn btn-danger"
                            style="float:right;margin-left:10px;"><i
                            class=" glyphicon glyphicon-remove"></i> 删除
                    </button>
                    <button id="showAddModalBtn" type="button" class="btn btn-primary" style="float:right;">
                        <i class="glyphicon glyphicon-plus"></i> 新增
                    </button>
                    <br>
                    <hr style="clear:both;">
                    <div class="table-responsive">
                        <table class="table  table-bordered">
                            <thead>
                            <tr>
                                <th width="30">#</th>
                                <th width="30"><input id="summaryBox" type="checkbox"></th>
                                <th>名称</th>
                                <th width="100">操作</th>
                            </tr>
                            </thead>
                            <tbody id="rolePageBody" class="rolePageBody">
                            <tr>
                                <td>8</td>
                                <td><input type="checkbox"></td>
                                <td>CMO / CMS - 配置管理员</td>
                                <td>
                                    <button type="button" class="btn btn-success btn-xs"><i
                                            class=" glyphicon glyphicon-check"></i></button>
                                    <button type="button" class="btn btn-primary btn-xs"><i
                                            class=" glyphicon glyphicon-pencil"></i></button>
                                    <button type="button" class="btn btn-danger btn-xs"><i
                                            class=" glyphicon glyphicon-remove"></i></button>
                                </td>
                            </tr>
                            </tbody>
                            <tfoot>
                            <tr>
                                <td colspan="6" align="center">
                                    <div id="Pagination" class="pagination"><!-- 这里显示分页 --></div>
                                </td>
                            </tr>

                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<%@include file="/WEB-INF/modal-role-add.jsp" %>
<%@include file="/WEB-INF/modal-role-update.jsp" %>
<%@include file="/WEB-INF/modal-role-reomve.jsp" %>
<%@include file="/WEB-INF/modal-role-assign-auth.jsp" %>
</html>